package org.abettor.pushbox.activity;

import java.util.ArrayList;
import java.util.List;

import org.abettor.pushbox.activity2.SiteMessageBoxActivity;
import org.abettor.pushbox.model.Pair;
import org.abettor.pushbox.notify.NotifyUtil;
import org.abettor.pushbox.receiver.TimerReceiver;
import org.abettor.pushbox.R;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PushBoxMainActivity extends Activity
{
	@Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.push_box_main_activity);
        GridView gridView = (GridView) findViewById(R.id.main_menu_grid_view);
        MainActivityAdapter adapter = new MainActivityAdapter();
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new OnItemClickListener());
        //注册定时器
        NotifyUtil.registerReceiverAlarm(this, TimerReceiver.class);
        //启动更新通知的sevice
        NotifyUtil.startNotifyService(this);
    }

	private class MainActivityAdapter extends BaseAdapter
	{
		private List<Pair<Integer, Integer>> itemList = new ArrayList<Pair<Integer, Integer>>();

		public MainActivityAdapter()
		{
			super();

			Pair<Integer, Integer> pair = new Pair<Integer, Integer>();
			pair.first = R.drawable.mod_btn_classic;
			pair.second = R.string.mod_classic_map;
			itemList.add(pair);

			pair = new Pair<Integer, Integer>();
			pair.first = R.drawable.mod_btn_private;
			pair.second = R.string.mod_private_map;
			itemList.add(pair);

			pair = new Pair<Integer, Integer>();
			pair.first = R.drawable.mod_btn_internet;
			pair.second = R.string.mod_internet_map;
			itemList.add(pair);

			pair = new Pair<Integer, Integer>();
			pair.first = R.drawable.mod_btn_question;
			pair.second = R.string.mod_question_map;
			itemList.add(pair);

			pair = new Pair<Integer, Integer>();
			pair.first = R.drawable.mod_btn_message;
			pair.second = R.string.mod_message;
			itemList.add(pair);

			pair = new Pair<Integer, Integer>();
			pair.first = R.drawable.mod_btn_setting;
			pair.second = R.string.mod_setting;
			itemList.add(pair);

			pair = new Pair<Integer, Integer>();
			pair.first = R.drawable.mod_btn_reply;
			pair.second = R.string.mod_reply;
			itemList.add(pair);

			pair = new Pair<Integer, Integer>();
			pair.first = R.drawable.mod_btn_help;
			pair.second = R.string.mod_help;
			itemList.add(pair);
		}

		@Override
		public int getCount()
		{
			return itemList.size();
		}

		@Override
		public Object getItem(int position)
		{
			return itemList.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			return itemList.get(position).first.longValue();
		}

		@Override
		public View getView(int position, View view, ViewGroup viewgroup)
		{
			Pair<Integer, Integer> pair = itemList.get(position);

			LinearLayout layout = new LinearLayout(PushBoxMainActivity.this);
			layout.setOrientation(LinearLayout.VERTICAL);
			//layout.setLayoutParams(params0);

			LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			params1.gravity = 0x01;
			ImageView imageView = new ImageView(PushBoxMainActivity.this);
			imageView.setBackgroundResource(pair.first);
			imageView.setLayoutParams(params1);

			LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			params2.gravity = 0x01;
			TextView textView = new TextView(PushBoxMainActivity.this);
			textView.setText(pair.second);
			textView.setTextColor(0xFFFFFFFF);
			textView.setLayoutParams(params2);

			layout.addView(imageView);
			layout.addView(textView);
			return layout;
		}
	}

	public class OnItemClickListener implements AdapterView.OnItemClickListener
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id)
		{
			Intent intent = new Intent();
			Class<?> clazz = null;
			switch(Long.valueOf(id).intValue())
			{
			case R.drawable.mod_btn_classic:
				clazz = ClassicMapActivity.class;
				break;
			case R.drawable.mod_btn_internet:
				clazz = NetUserMapActivity.class;
				break;
			case R.drawable.mod_btn_question:
				clazz = QuesstionMapActivity.class;
				break;
			case R.drawable.mod_btn_private:
				clazz = SelfmakeMapActivity.class;
				break;
			case R.drawable.mod_btn_setting:
				clazz = OptionActivity.class;
				break;
			case R.drawable.mod_btn_reply:
				clazz = ContactUsActivity.class;
				break;
			case R.drawable.mod_btn_message:
				clazz = SiteMessageBoxActivity.class;
				break;
			case R.drawable.mod_btn_help:
				showHelp();
				return;
			}
			if(clazz != null)
			{
				intent.setClass(PushBoxMainActivity.this, clazz);
				startActivity(intent);
			}
		}
	}

	//	显示帮助对话框
	private void showHelp()
	{
		Builder builder = new Builder(this);
		builder.setCancelable(true);
		builder.setTitle(R.string.help_dialog_title);
		builder.setMessage(R.string.help_dialog_content);
		builder.show();
	}
}
